package org.steve.betterattack;

import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CraftingRecipe {
    final int nowLevel;
    final String id;
    final BetterAttack plugin;
    final String[]items;
    final String result;
    String targetID =null;
    public final boolean isOrdered;
    public final boolean isUpgrade;
    private boolean compare(String[]s1,String[]s2){
        List<String>i2 = new ArrayList<>(Arrays.asList(s2));
        for (String s : s1) {
            if(i2.contains(s.toLowerCase())||i2.contains(s.toUpperCase())){
                i2.remove(s);
            }
            else {
                return false;
            }
        }
        return true;
    }
    public ItemStack getItemStack(){
        if(isUpgrade){
            return plugin.RegisteredWeapons.get(targetID).getItemStack(nowLevel+1);
        }
        else{
            if(result.indexOf("ba.")==0){
                String target=result.substring(3);
                if (plugin.RegisteredWeapons.containsKey(target)){
                    return plugin.RegisteredWeapons.get(target).getItemStack(0);
                }
                else {
                    return plugin.RegisteredItems.get(target).getItemStack();
                }
            }
            return new ItemStack(Material.valueOf(result.toUpperCase()));
        }
    }
    public boolean isThis(ItemStack[] itemStacks){
        String[]is=new String[9];
        for (int i = 0; i < is.length; i++) {
            if (itemStacks[i]==null){
                is[i]="air";
            }
            else if (itemStacks[i].getItemMeta().getPersistentDataContainer().get(new NamespacedKey(plugin,"id"), PersistentDataType.STRING)!=null){
                is[i]="ba."+itemStacks[i].getItemMeta().getPersistentDataContainer().get(new NamespacedKey(plugin,"id"), PersistentDataType.STRING);
            }
            else {
                is[i]=itemStacks[i].getType().name().toLowerCase();
            }
        }

        if(isUpgrade){
            for (int i = 0; i < is.length; i++) {
                if(is[i]!=null && is[i].indexOf("ba.") == 0) {
                    if (is[i].substring(3).equalsIgnoreCase(targetID)) {
                        if (itemStacks[i].getItemMeta().getPersistentDataContainer().get(new NamespacedKey(plugin, "level"), PersistentDataType.INTEGER) != nowLevel) {
                            return false;
                        }
                    }
                }
            }
            return compare(items,is);
        }
        else if(isOrdered){
            boolean y=true;
            for (int i = 0; i < 9; i++) {
                if (is[i]!=items[i])
                    y = false;
            }
            return y;
        }
        else{
            return compare(items,is);
        }
    }
    public CraftingRecipe(BetterAttack plugin, ConfigurationSection config){
        nowLevel=0;
        id=config.getName();
        isUpgrade=false;
        this.plugin=plugin;
        items=new String[9];
        result=config.getString("result");
        for (int i=0;i<9;i++){
            items[i]=config.getString(Integer.toString(i));
        }
        isOrdered=config.getBoolean("ordered");
    }
    public CraftingRecipe(BetterAttack plugin, String id, ArrayList<String> items, int nowLevel){
        this.nowLevel=nowLevel;
        this.id="_betterattack_"+id+nowLevel;
        targetID =id;
        this.items=new String[9];
        isUpgrade=true;
        isOrdered=false;
        for (int i=0;i<items.size();i++){
            //plugin.getLogger().info(items.getClass().getTypeName());
            this.items[i]= items.get(i);
            if (this.items[i]==null){
                this.items[i]="air";
            }
        }
        result=null;
        this.plugin=plugin;
    }

    public String getInfo(){
        String ret = "";
        ret+="id: "+id+"\n";
        ret+="isUpgrade: "+isUpgrade+"\n";
        ret+="isOrdered: "+isOrdered+"\n";
        ret+="items\n";
        for (String str:items){
            ret+=str+"\n";
        }
        if (isUpgrade){
            ret+="nowLevel: "+nowLevel+"\n";
            ret+="targetID: "+ targetID +"\n";
        }
        else {
            ret += "result: " + result + "\n";
        }
        return ret;
    }
}
